Skip to content

added timed drain filter to prevent garbage input after attach#39

Open
miranda wants to merge 1 commit into
mobydeck:mainfrom
miranda:timed-drain-filter
Open

added timed drain filter to prevent garbage input after attach#39
miranda wants to merge 1 commit into
mobydeck:mainfrom
miranda:timed-drain-filter

Conversation

@miranda
Copy link
Copy Markdown

@miranda miranda commented May 29, 2026

Problem:

When reattaching to a session running a program that uses terminal query sequences (vim, fish, etc.) the on-disk log replay writes raw escape sequences to the new terminal. atch forwards those responses to the pty as keyboard input, causing the program to interpret them as typed characters, resulting in junk input. This is easily demonstrated by detaching and reattaching a session running vim, where it will paste junk from the clipboard immediately on attach.

Fix:

After switching to raw mode and before sending MSG_ATTACH / MSG_REDRAW, run a short drain loop (default 100ms) that reads and discards anything arriving on stdin. This swallows the terminal's responses to replayed query sequences. It only watches stdin (not the socket), so it cannot interfere with pty data.

Placing the drain before MSG_ATTACH and MSG_REDRAW is critical: the SIGWINCH triggered by MSG_REDRAW causes programs like fish to redraw their prompts, which involves sending new terminal queries and expecting responses. If the drain ran during the main select loop, it would swallow those legitimate responses and cause the program to hang waiting for them.

The drain duration can be overridden with ATCH_DRAIN_MS (e.g. ATCH_DRAIN_MS=200) or disabled entirely with ATCH_DRAIN_MS=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant